package org.pgis.gui;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;

import org.pgis.core.AbstractScenePanel;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;

public class ScalePanel extends JPanel{

	private static final long serialVersionUID = 1L;
	private AbstractScenePanel asp;
	private JTextField textX;
	private JTextField textY;
	private JTextField textZ;

	public ScalePanel(AbstractScenePanel asp) {
		this.asp = asp;
		initialize();
	}

	private void initialize() {

		this.setLayout(null);

		JLabel labelColor = new JLabel("Scale");
		labelColor.setFont(new Font("Century", Font.BOLD, 16));
		labelColor.setBounds(87, 11, 81, 30);
		this.add(labelColor);

		JLabel labelStart = new JLabel("X");
		labelStart.setBounds(21, 65, 46, 14);
		add(labelStart);

		JLabel labelEnd = new JLabel("Y");
		labelEnd.setBounds(21, 105, 46, 14);
		add(labelEnd);

		JLabel labelDistance = new JLabel("Z");
		labelDistance.setBounds(21, 145, 46, 14);
		add(labelDistance);

		JLabel labelEndValue = new JLabel("");
		labelEndValue.setBounds(95, 135, 137, 14);
		add(labelEndValue);

		textX = new JTextField();
		textX.setText("1.0");
		textX.setBounds(62, 57, 95, 30);
		add(textX);
		textX.setColumns(10);

		textY = new JTextField();
		textY.setText("1.0");
		textY.setBounds(62, 97, 95, 30);
		add(textY);
		textY.setColumns(10);

		textZ = new JTextField();
		textZ.setText("1.0");
		textZ.setBounds(62, 135, 95, 30);
		add(textZ);
		textZ.setColumns(10);

		JButton buttonApply = new JButton("Apply");
		buttonApply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				float x=1, y = 1, z = 1;

				try {
					if(!textX.getText().isEmpty()){
						x = Float.valueOf(textX.getText());
					} else {
						x = 1.0f;
					}

					if(!textY.getText().isEmpty()){
						y = Float.valueOf(textY.getText());
					} else {
						y = 1.0f;
					}

					if(!textZ.getText().isEmpty()){
						z = Float.valueOf(textZ.getText());
					} else {
						z = 1.0f;
					}

				}catch(NumberFormatException ex) {
					ex.printStackTrace();
				}

				asp.getSim().setScale(x, y, z);
				asp.refresh();
			}
		});
		buttonApply.setBounds(62, 173, 95, 23);
		add(buttonApply);
	}
}
